#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
const char* path = "/usr/IPC";
int main()
{
	int data[5] = {1,2,3,4,5};
	int i = 0;
	key_t key;
	int shmId;
	
	key = ftok(path, 1); //为共享内存生成键值
	if(key == -1)
	{
		cout << "ftok failed" << endl;
	}
	
	shmId = shmget(key, 100, IPC_CREAT|IPC_EXCL | 0600); //获取共享内存标志符
	cout <<"shmId=" << shmId << endl;
	if(shmId == -1)
	{
		cout << "shmget failed" << endl;
	}
	
	int* shmaddr = (int*)shmat(shmId, NULL, 0); //获取共享内存地址
	for(i = 0; i < 5; i++)
	{
	//往共享内存写数据
		*shmaddr = data[i];
		shmaddr++;
	}
	cout << "write end" << endl;
	return 0;
}
